<template>
  <div class="admin-log-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>用户日志</span>
      </div>
      
      <!-- 添加筛选条件 -->
      <div class="filter-container">
        <!-- <el-select style="margin-right:30px" v-model="selectedAdmin" placeholder="选择APP" clearable @change="handleFilter">
          <el-option
            v-for="item in adminList"
            :key="item.id"
            :label="item.nickname"
            :value="item.id">
          </el-option>
        </el-select> -->

        <el-date-picker
      v-model="value1"
      type="daterange"
      @change="handleDateChange"

      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期">
    </el-date-picker>
      </div>

      <div class="app_count">

        <span>
            星球客调用总数 {{  xqk_count  }}
        </span>
        <span>
            有球体育调用总数 {{  yqty_count  }}
        </span>
        <span>
            微信小程序调用总数 {{  wechat_count  }}
        </span>
      </div>

      <el-table
        :data="logList"
        style="width: 100%"
        border
        v-loading="loading">
        <el-table-column
          prop="id"
          label="ID"
          width="80">
        </el-table-column>
        <el-table-column
          prop="appname"
          label="应用名称"
          width="80">

            <template slot-scope="scope">
                {{  appFilter(scope.row.appname)  }}
            </template>

        </el-table-column>
        <el-table-column
          prop="platform"
          label="平台"
          width="80">

            <template slot-scope="scope">
                {{  ptFilter(scope.row.platform)  }}
            </template>

        </el-table-column>
        <el-table-column
          prop="user.nickname"
          label="用户名"
          >
        </el-table-column>
         <el-table-column
          prop="user.remark"
          label="备注"
          >
        </el-table-column>
        <el-table-column
          prop="ip"
          label="IP地址"
          width="140">
        </el-table-column>
        <el-table-column
          prop="count"
          width="140"
          label="接口调用次数">
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="最后调用时间"
          width="180">
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import {   userLog as getAdminLog , getAdminList } from '@/api/log'
export default {
  name: 'AdminLog',
  data() {
    return {
      logList: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      selectedAdmin: '', // 新增：选中的管理员ID
      adminList: [
            {
                nickname: '星球客',
                id: 'xqk'
            },
            {
                nickname: '有球体育',
                id: 'yqty'
            },
            {
                nickname: '310大师',
                id: '310'
            }
      ],
      wechat_count: 0,
      xqk_count: 0,
      yqty_count: 0,
      orderTime: '',
      value1: ''      
    }
  },
  methods: {
    handleDateChange(value){
            console.log(value)
          this.currentPage = 1 

            this.orderTime = value[0]+"_"+value[1]
            const newDate1 = value[0].getFullYear() + '-' + (value[0].getMonth() + 1) + '-' + value[0].getDate();
            const newDate2 = value[1].getFullYear() + '-' + (value[1].getMonth() + 1) + '-' + value[1].getDate();
            console.log(newDate1+'-'+newDate2)
            this.orderTime = newDate1+'_'+newDate2
            this.getLogList()
        },
    ptFilter(txt) {
        let name = ''
        switch(txt) {
            case 'app':
                name = 'APP'
                break;
            case 'mp-weixin':
                name = '小程序'
                break;
                case 'web':
                name = 'H5'
                break;    
            default:
                name = '其他'
                break;            
        }

        return name
    },
    appFilter(txt) {
        let name = '星球客'
        switch(txt) {
            case 'xqk':
                name = '星球客'
                break;
            case '310':
            case '310Android':
                name = '310大师'
                break;
            default:
                name = '有球体育'
                break;            
        }

        return name
    },
    getAdminList() {

    },
    // 获取日志列表数据
    getLogList() {
      this.loading = true
      // 这里替换为实际的API调用
      getAdminLog({
          page: this.currentPage,
          page_size: this.pageSize,
          order_time: this.orderTime
        }).then(res => {
            console.log()
        this.logList = res.data.data
        this.total = res.data.last_page
        this.wechat_count = res.data.wechat_count
        this.xqk_count = res.data.xqk_count
        this.yqty_count = res.data.yqty_count
        this.loading = false
      })
    },
    // 新增：处理筛选变化
    handleFilter() {
      this.currentPage = 1 // 重置到第一页
      this.getLogList()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getLogList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getLogList()
    }
  },
  created() {
    this.getAdminList()
    this.getLogList()
  }
}
</script>

<style scoped>
.admin-log-container {
  padding: 20px;
}
.filter-container {
  margin-bottom: 20px;
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
.app_count {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
}
.app_count span {
    color: red;
    margin-right: 20px;
    font-size: 14px;
}
</style>